package com.sheng.leetcode.year2023.month04.day14;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liusheng
 * @date 2023/04/
 * <p>
 * 1023. 驼峰式匹配<p>
 * <p>
 * 如果我们可以将小写字母插入模式串 pattern 得到待查询项 query，那么待查询项与给定模式串匹配。<p>
 * （我们可以在任何位置插入每个字符，也可以插入 0 个字符。）<p>
 * 给定待查询列表 queries，和模式串 pattern，返回由布尔值组成的答案列表 answer。<p>
 * 只有在待查项 queries[i] 与模式串 pattern 匹配时， answer[i] 才为 true，否则为 false。<p>
 * <p>
 * 示例 1：<p>
 * 输入：queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FB"<p>
 * 输出：[true,false,true,true,false]<p>
 * 示例：<p>
 * "FooBar" 可以这样生成："F" + "oo" + "B" + "ar"。<p>
 * "FootBall" 可以这样生成："F" + "oot" + "B" + "all".<p>
 * "FrameBuffer" 可以这样生成："F" + "rame" + "B" + "uffer".<p>
 * <p>
 * 示例 2：<p>
 * 输入：queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBa"<p>
 * 输出：[true,false,true,false,false]<p>
 * 解释：<p>
 * "FooBar" 可以这样生成："Fo" + "o" + "Ba" + "r".<p>
 * "FootBall" 可以这样生成："Fo" + "ot" + "Ba" + "ll".<p>
 * <p>
 * 示例 3：<p>
 * 输入：queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBaT"<p>
 * 输出：[false,true,false,false,false]<p>
 * 解释：<p>
 * "FooBarTest" 可以这样生成："Fo" + "o" + "Ba" + "r" + "T" + "est".<p>
 * <p>
 * 提示：<p>
 * 1 <= queries.length <= 100<p>
 * 1 <= queries[i].length <= 100<p>
 * 1 <= pattern.length <= 100<p>
 * 所有字符串都仅由大写和小写英文字母组成。<p>
 * <p>
 * 来源：力扣（LeetCode）<p>
 * 链接：<a href="https://leetcode.cn/problems/camelcase-matching">1023. 驼峰式匹配</a><p>
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。<p>
 */
public class LeetCode1023 {

    @Test
    public void test01() {
        String[] queries = {"FooBar", "FooBarTest", "FootBall", "FrameBuffer", "ForceFeedBack"};
//        String pattern = "FB";
//        String pattern = "FoBa";
        String pattern = "FoBaT";
        System.out.println(new Solution().camelMatch(queries, pattern));
    }
}

class Solution {
    public List<Boolean> camelMatch(String[] queries, String pattern) {
        List<Boolean> booleans = new ArrayList<>();
        int n = pattern.length();
        for (String query : queries) {
            int m = query.length();
            // i 和 j 分别指向 query 和 pattern 字符串
            int i = 0, j = 0;
            boolean flag = true;
            for (; j < n; i++, j++) {
                while (i < m && query.charAt(i) != pattern.charAt(j) && Character.isLowerCase(query.charAt(i))) {
                    i++;
                }
                if (i == m || query.charAt(i) != pattern.charAt(j)) {
                    booleans.add(false);
                    flag = false;
                    break;
                }
            }
            if (flag) {
                while (i < m && Character.isLowerCase(query.charAt(i))) {
                    i++;
                }
                booleans.add(i == m);
            }
        }
        return booleans;
    }
}
